package com.ke.jun.bg.testpetpackmvvm.ui.activity

import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.WindowManager
import android.widget.Toast
import androidx.activity.OnBackPressedCallback
import androidx.navigation.Navigation
import com.blankj.utilcode.util.ToastUtils
import com.ke.jun.bg.petpackmvvm.network.manager.NetState
import com.ke.jun.bg.testpetpackmvvm.R
import com.ke.jun.bg.testpetpackmvvm.app.appViewModel
import com.ke.jun.bg.testpetpackmvvm.app.base.BaseActivity
import com.ke.jun.bg.testpetpackmvvm.app.util.StatusBarUtil
import com.ke.jun.bg.testpetpackmvvm.databinding.ActivityMainBinding
import com.ke.jun.bg.testpetpackmvvm.viewmodel.state.MainViewModel
import com.tencent.bugly.beta.Beta


/**
 * 项目的主要Activity
 */
class MainActivity : BaseActivity<MainViewModel,ActivityMainBinding>() {
    var exitTime = 0L
    override fun layoutId()= R.layout.activity_main

    override fun initView(savedInstanceState: Bundle?) {
        //进入首页检测更新
        Beta.checkAppUpgrade(false,true)
        onBackPressedDispatcher.addCallback(this,object :OnBackPressedCallback(true){
            override fun handleOnBackPressed() {
                val nav=Navigation.findNavController(this@MainActivity, R.id.host_fragment)
                if (nav.currentDestination!=null&& nav.currentDestination!!.id!= R.id.mainFragmnet){
                    //如果当前的界面不是主页，那么直接返回即可
                    nav.navigateUp()
                }else{
                    //是主页
                    if (System.currentTimeMillis()-exitTime>2000){
                        ToastUtils.showShort("再按一次就退出了")
                        exitTime=System.currentTimeMillis()
                    }else{
                        finish()
                    }
                }
            }

        })
        appViewModel.appColor.value?.let {
            window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
            supportActionBar?.setBackgroundDrawable(ColorDrawable(it))
            StatusBarUtil.setColor(this,it,0)
        }
    }

    override fun createObserver() {
        appViewModel.appColor.observeInActivity(this) {
             supportActionBar?.setBackgroundDrawable(ColorDrawable(it))
            StatusBarUtil.setColor(this,it,0)
        }
    }

    override fun onNetworkStateChanged(netState: NetState?) {
        if (netState?.isSuccess == true){
            Toast.makeText(applicationContext, "我特么终于有网了啊!", Toast.LENGTH_SHORT).show()
        }else{
            Toast.makeText(applicationContext, "我特么怎么断网了!", Toast.LENGTH_SHORT).show()
        }
    }



}